package com.az;

import static org.jinterop.dcom.core.JIProgId.valueOf;
import static org.jinterop.dcom.impls.JIObjectFactory.narrowObject;
import static org.jinterop.dcom.impls.automation.IJIDispatch.IID;

import java.applet.Applet;
import java.util.logging.Level;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.common.JISystem;
import org.jinterop.dcom.core.IJIComObject;
import org.jinterop.dcom.core.JIArray;
import org.jinterop.dcom.core.JIComServer;
import org.jinterop.dcom.core.JISession;
import org.jinterop.dcom.core.JIString;
import org.jinterop.dcom.core.JIVariant;
import org.jinterop.dcom.impls.automation.IJIDispatch;
import org.jinterop.dcom.impls.automation.IJIEnumVariant;

public class PerformanceMonitor extends Applet {

	String domain = "macrosoftinc.com.pk";
	String hostname = "mac-pk-ahaider";
	String username = "aizaz.haider";
	String password = "hello";

	public JIComServer comServer = null;

	public JISession dcomSession = null;

	public IJIDispatch wbemServices = null;

	public Object[] params = null;

	public static final String diskMonitors = "Win32_PerfFormattedData_PerfDisk_LogicalDisk";

	public static final String operatingSystem = "Win32_OperatingSystem";

	public static final String memoryMonitors = "Win32_PerfRawData_PerfOS_Memory";

	public static final String procMonit = "Win32_PerfFormattedData_PerfOS_Processor";

	public static final String systemMonit = "Win32_PerfFormattedData_PerfOS_Processor";
	
	//--az
	public static final String soundCardInfo = "Win32_SoundDevice"; 
			
	//---

	public String memoryStatus = "Win32_OperatingSystem";

	public String cpuUsage = "Win32_PerfFormattedData_PerfProc_Process";

	public String windowsDiskInfo = "Win32_LogicalDisk";	

	private long totalDiskUsage = 0;

	private long freeDiskUsage = 0;

	private PerfMonitBean perfMonitBean = new PerfMonitBean();

	public PerformanceMonitor() {

	}

	// public PerfMonitBean getPerformanceMonitInfo() {
	// return perfMonitBean;
	// }

	@Override
	public void init() {		
		try {
			PerformanceMonitor performanceMonitor = new PerformanceMonitor();
			performanceMonitor.setWindowsCredentials(performanceMonitor.domain, performanceMonitor.hostname, performanceMonitor.username, performanceMonitor.password);

			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					JLabel labale = new JLabel("Test");
					add(labale);
				};
			});
		} catch (Exception ex) {
			ex.printStackTrace();
		}		
	}

	public void setWindowsCredentials(String domain, String hostname, String username, String password) {
		try {
			dcomSession = init(domain, username, password);
			comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"), hostname, dcomSession);
			IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));

			params = new Object[] { new JIString(hostname), new JIString("ROOT\\CIMV2"), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),
					JIVariant.OPTIONAL_PARAM(), new Integer(0), JIVariant.OPTIONAL_PARAM() };
			JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);
			wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());

			getWMIDiskMonitors(getJIVariants(wbemServices, diskMonitors));

			getWMIOperatingSystem(getJIVariants(wbemServices, operatingSystem));

			getWMIMemoryMonitors(getJIVariants(wbemServices, memoryMonitors));

			getWMIProcessMonitors(getJIVariants(wbemServices, procMonit));

			getServerMonitors(getJIVariants(wbemServices, procMonit));

			getMemoryUtilization(getJIVariants(wbemServices, memoryStatus));

			getCPUUsage(getJIVariants(wbemServices, cpuUsage));

			getWindowsDiskInfo(getJIVariants((wbemServices), windowsDiskInfo));
			
			getWMISoundCard(getJIVariants(wbemServices, soundCardInfo));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dcomSession != null) {
				try {
					JISession.destroySession(dcomSession);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}

	}

	public void getWindowsDiskInfo(JIVariant[] servicesSet) {
		try {

			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();
			StringBuffer diskInfo = null;
			long usedSpace = 0;
			diskInfo = new StringBuffer();
			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					// Use other methods to start and stop services. For now
					// we'll just print as text.
					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));

					JIVariant driveType = (JIVariant) (wbemObjectDispatch.get("DriveType"));

					JIVariant size = (JIVariant) (wbemObjectDispatch.get("Size"));

					JIVariant freeSpace = (JIVariant) (wbemObjectDispatch.get("FreeSpace"));

					if (driveType.getObjectAsInt() == 3) {

						long totalSpaceTemp = Long.parseLong(size.getObjectAsString2());

						// 1GB = 1073741824
						// 1MB = 1048576;
						Long totalSpace = totalSpaceTemp;

						long freeSpaceTemp = Long.parseLong(freeSpace.getObjectAsString2());

						usedSpace = totalSpaceTemp - freeSpaceTemp;

						diskInfo.append(name.getObjectAsString2());

						diskInfo.append(",");
						System.out.println("PerformanceMonitor.getWindowsDiskInfo()usedSpace=" + usedSpace);

						System.out.println("PerformanceMonitor.getWindowsDiskInfo()totalSpaceTemp=" + totalSpaceTemp);

						float usage = (float) usedSpace / totalSpaceTemp;

						int usageper = (int) (usage * 100);

						diskInfo.append("Used=" + String.valueOf(usageper).toString() + "%");
						diskInfo.append(";");

						totalDiskUsage = totalDiskUsage + totalSpaceTemp;

						freeDiskUsage = freeDiskUsage + freeSpaceTemp;

						usedSpace = totalDiskUsage - freeDiskUsage;

					}

				}
			}

			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setDiskUtilization=" + diskInfo.toString());
			// perfMonitBean.setPartitionDetailsoftheDeviceInPercentage(diskInfo.toString());

			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setDiskUtilization=" + String.valueOf(usedSpace).toString() + "KB");
			// perfMonitBean.setDiskUtilization(String.valueOf(usedSpace).toString() + "KB");
			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setFreeDiskSpaceInGB=" + String.valueOf(freeDiskUsage / 1073741824).toString() + "GB");

			// perfMonitBean.setFreeDiskSpaceInGB(String.valueOf(freeDiskUsage / 1073741824).toString() + "GB");
			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setFreeDiskSpaceInMB=" + String.valueOf(freeDiskUsage / 1048576).toString() + "MB");

			// perfMonitBean.setFreeDiskSpaceInMB(String.valueOf(freeDiskUsage / 1048576).toString() + "MB");

			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setUsedDiskSpaceInGB=" + String.valueOf(usedSpace / 1073741824).toString() + "GB");

			// perfMonitBean.setUsedDiskSpaceinGB(String.valueOf(usedSpace / 1073741824).toString() + "GB");
			System.out.println("PerformanceMonitor.getWindowsDiskInfo()setUsedDiskSpaceInMB=" + String.valueOf(usedSpace / 1048576).toString() + "MB");

			// perfMonitBean.setUsedDiskSpaceinMB(String.valueOf(usedSpace / 1048576).toString() + "MB");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getCPUUsage(JIVariant[] cpuUsage) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(cpuUsage[0].getObjectAsComObject());
			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));
			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			int perCpuUsage = 0;

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));
					if (!(name.equals("_Total") || name.equals("Idle"))) {
						JIVariant perProcTime = (JIVariant) (wbemObjectDispatch.get("PercentProcessorTime"));
						perCpuUsage += Integer.parseInt(perProcTime.getObjectAsString2());

					}
				}
			}

			System.out.println("WindowsOS.getCPUUsage()Total CPU Usage=" + perCpuUsage / numberOfServices + "%");
			// perfMonitBean.setCpuUtilization(perCpuUsage / numberOfServices + "%");
		} catch (JIException e) {
			e.printStackTrace();
		}

	}

	public void getMemoryUtilization(JIVariant[] memoryUtil) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(memoryUtil[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			long usedMemory = 0;
			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant totalPhyMem = (JIVariant) (wbemObjectDispatch.get("TotalVisibleMemorySize"));

					long totalPhysicalMemory = Long.valueOf(totalPhyMem.getObjectAsString2());

					System.out.println("WindowsOS.getMemoryUtilization(TotalVisibleMemorySize)=" + totalPhyMem.getObjectAsString2());

					JIVariant freePhyMem = (JIVariant) (wbemObjectDispatch.get("FreePhysicalMemory"));

					long freePhysicalMemory = Long.valueOf(freePhyMem.getObjectAsString2());

					System.out.println("WindowsOS.getMemoryUtilization(TotalFreeMemorySize)=" + freePhyMem.getObjectAsString2());

					usedMemory = totalPhysicalMemory - freePhysicalMemory;

					System.out.println("WindowsOS.getMemoryUtilization(usedMemory)" + usedMemory);

				}

			}
			System.out.println("WindowsOS.getMemoryUtilization(MemoryUsage)" + usedMemory);
			// perfMonitBean.setMemoryUtilization(String.valueOf(usedMemory).toString());
		} catch (JIException e) {
			e.printStackTrace();
		}

	}

	private void getSystemMonitors(JIVariant[] systemMonit) {
		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(systemMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			StringBuffer percPrivTime = new StringBuffer();

			StringBuffer perProcessorTime = new StringBuffer();

			StringBuffer perUserTime = new StringBuffer();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));
				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}
	}

	private void getWMIMonitors(JIVariant[] wmiMonit) {
		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(wmiMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));
				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}
	}

	private void getServerMonitors(JIVariant[] serverMonit) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(serverMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			StringBuffer percPrivTime = new StringBuffer();

			StringBuffer perProcessorTime = new StringBuffer();

			StringBuffer perUserTime = new StringBuffer();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));
				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}

	}

	private void getWMIProcessMonitors(JIVariant[] procMonit) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(procMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			StringBuffer percPrivTime = new StringBuffer();

			StringBuffer perProcessorTime = new StringBuffer();

			StringBuffer perUserTime = new StringBuffer();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant name = (JIVariant) (wbemObjectDispatch.get("Name"));

					if (name.equals("_Total") || name.equals("Idle")) {

					} else {
						JIVariant percentProcessorTime = (JIVariant) (wbemObjectDispatch.get("PercentProcessorTime"));

						// JIVariant percentIdleTime = (JIVariant)
						// (wbemObjectDispatch
						// .get("PercentIdleTime"));

						JIVariant percentPrivilegedTime = (JIVariant) (wbemObjectDispatch.get("PercentPrivilegedTime"));

						JIVariant percentUserTime = (JIVariant) (wbemObjectDispatch.get("PercentUserTime"));

						// perfMonitBean.setIdleTime(String.valueOf(
						// percentIdleTime.getObjectAsInt()).toString());

						percPrivTime.append(name.getObjectAsString2()).append(":").append(percentPrivilegedTime.getObjectAsString2());

						percPrivTime.append("\n");

						perProcessorTime.append(name.getObjectAsString2()).append(":").append(percentProcessorTime.getObjectAsString2());

						perProcessorTime.append("\n");

						perUserTime.append(name.getObjectAsString2()).append(":").append(percentUserTime.getObjectAsString2());

						perUserTime.append("\n");

						// System.out
						// .println("PerformanceMonitor.getWMIProcessMonitors()PercentIdleTime="
						// + String.valueOf(
						// percentIdleTime
						// .getObjectAsInt())
						// .toString());

					}

				}

			}

			System.out.println("PerformanceMonitor.getWMIProcessMonitors()PercentPrivilegedTime=" + percPrivTime.toString());

			System.out.println("PerformanceMonitor.getWMIProcessMonitors()PercentProcessorTime=" + perProcessorTime.toString());

			System.out.println("PerformanceMonitor.getWMIProcessMonitors()PercentUserTime=" + perUserTime.toString());

			// perfMonitBean.setPrivilegedTime(percPrivTime.toString());

			// perfMonitBean.setProcessorTime(perProcessorTime.toString());

			// perfMonitBean.setUserTime(perUserTime.toString());

		} catch (JIException e) {
			e.printStackTrace();
		}
	}

	private void getWMIOperatingSystem(JIVariant[] operSystem) {
		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(operSystem[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant freePhysicalMem = (JIVariant) (wbemObjectDispatch.get("FreePhysicalMemory"));

					System.out.println("PerformanceMonitor.getWMIOperatingSystem()FreePhysicalMemory=" + freePhysicalMem.getObjectAsString2());
					// perfMonitBean.setFreePhysicalMemory(freePhysicalMem.getObjectAsString2());

				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}
	}

	private void getWMIMemoryMonitors(JIVariant[] memMonit) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(memMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant pageFaultsPerSec = (JIVariant) (wbemObjectDispatch.get("PageFaultsPerSec"));

					JIVariant pageReadsPerSec = (JIVariant) (wbemObjectDispatch.get("PageReadsPerSec"));

					JIVariant pageWritesPerSec = (JIVariant) (wbemObjectDispatch.get("PageWritesPerSec"));

					JIVariant pagesPerSec = (JIVariant) (wbemObjectDispatch.get("PagesPerSec"));

					System.out.println("PerformanceMonitor.getWMIMemoryMonitors()PageFaultsPerSec=" + pageFaultsPerSec.getObjectAsInt());

					// perfMonitBean.setPageFaults(String.valueOf(pageFaultsPerSec.getObjectAsInt()).toString());

					System.out.println("PerformanceMonitor.getWMIMemoryMonitors()PageReadsPerSec=" + pageReadsPerSec.getObjectAsInt());

					// perfMonitBean.setPageReads(String.valueOf(pageReadsPerSec.getObjectAsInt()).toString());

					System.out.println("PerformanceMonitor.getWMIMemoryMonitors()PageWritesPerSec=" + pageWritesPerSec.getObjectAsInt());

					// perfMonitBean.setPageWrites(String.valueOf(pageWritesPerSec.getObjectAsInt()).toString());

					System.out.println("PerformanceMonitor.getWMIMemoryMonitors()PagesPerSec=" + pagesPerSec.getObjectAsInt());

					// perfMonitBean.setPageWrites(String.valueOf(pagesPerSec.getObjectAsInt()).toString());

				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}

	}

	private void getWMIDiskMonitors(JIVariant[] diskMonit) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(diskMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant diskReadBytesPerSec = (JIVariant) (wbemObjectDispatch.get("DiskReadBytesPerSec"));

					System.out.println("PerformanceMonitor.getWMIOperatingSystem()DiskReadBytesPerSec=" + diskReadBytesPerSec.getObjectAsString2());
//					perfMonitBean.setDiskReads(String.valueOf(diskReadBytesPerSec.getObjectAsString2()));
					

					JIVariant diskWriteBytesPerSec = (JIVariant) (wbemObjectDispatch.get("DiskWriteBytesPerSec"));

					System.out.println("PerformanceMonitor.getWMIOperatingSystem()DiskWriteBytesPerSec=" + diskWriteBytesPerSec.getObjectAsString2());
//					perfMonitBean.setDiskWrites(String.valueOf(diskWriteBytesPerSec.getObjectAsString2()));

					JIVariant currentDiskQueueLength = (JIVariant) (wbemObjectDispatch.get("CurrentDiskQueueLength"));

					System.out.println("PerformanceMonitor.getWMIOperatingSystem()CurrentDiskQueueLength=" + currentDiskQueueLength.getObjectAsInt());
//					perfMonitBean.setDiskQueueLength(String.valueOf(currentDiskQueueLength.getObjectAsInt()).toString());

				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}
	}
	
	private void getWMISoundCard(JIVariant[] diskMonit) {

		try {
			IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(diskMonit[0].getObjectAsComObject());

			JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
			IJIComObject object2 = newEnumvariant.getObjectAsComObject();
			IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));

			JIVariant countVariant = wbemObjectSet.get("Count");
			int numberOfServices = countVariant.getObjectAsInt();

			for (int i = 0; i < numberOfServices; i++) {
				Object[] elements = enumVARIANT.next(1);
				JIArray aJIArray = (JIArray) elements[0];

				JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
				for (JIVariant variant : array) {
					IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

					JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });

					JIVariant deviceID = (JIVariant) (wbemObjectDispatch.get("DeviceID"));
					
					System.out.println("SoundCard Device ID =" + deviceID.getObjectAsString2());
					perfMonitBean.setHardwareID(deviceID.getObjectAsString2());

				}
			}
		} catch (JIException e) {
			e.printStackTrace();
		}
	}

	public JIVariant[] getJIVariants(IJIDispatch wbemServices, String task) {
		JIVariant[] arrJIVariant = null;
		try {
			params = new Object[] { new JIString(task), new Integer(0), JIVariant.OPTIONAL_PARAM() };

			arrJIVariant = wbemServices.callMethodA("InstancesOf", params);
		} catch (JIException e) {
			e.printStackTrace();
		}
		return arrJIVariant;
	}

	private static JISession init(String domain, String user, String pass) throws Exception {
		JISystem.getLogger().setLevel(Level.OFF);
		JISystem.setAutoRegisteration(true);

		JISession dcomSession = JISession.createSession(domain, user, pass);
		dcomSession.useSessionSecurity(false);
		return dcomSession;
	}

	public static void main(String[] args) {
		PerformanceMonitor performanceMonitor = new PerformanceMonitor();
		performanceMonitor.setWindowsCredentials(performanceMonitor.domain, performanceMonitor.hostname, performanceMonitor.username, performanceMonitor.password);
	}

}
